package com.sg.business.commons.ui.work;

import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PlatformUI;

import com.mobnut.db.model.PrimaryObject;
import com.sg.business.model.Work;
import com.sg.widgets.commons.model.IEditorInputFactory;
import com.sg.widgets.part.editor.DataObjectView;
import com.sg.widgets.part.editor.PrimaryObjectEditorInput;

public class WorkEditorView extends DataObjectView {

	private IWorkbenchPart activePart;

	public void setActivePart(IWorkbenchPart activePart) {
		this.activePart = activePart;
	}

	@Override
	public void goHome() {
		setWorkInput(null);
		cleanUI();
		IWorkbenchPage page = PlatformUI.getWorkbench()
				.getActiveWorkbenchWindow().getActivePage();
		if (activePart != null) {
			page.activate(activePart);
		}
	}

	public void setWorkInput(Work work) {
		cleanInput();
		if (work != null) {
			IEditorInputFactory f = work.getAdapter(IEditorInputFactory.class);
			PrimaryObjectEditorInput newInput = f.getInput("navigate");
			setInput(newInput);
		}
	}

	@Override
	public void setInput(PrimaryObjectEditorInput newInput) {
		if (newInput != null) {
			super.setInput(newInput);
		}
	}

	@Override
	protected PrimaryObjectEditorInput getInput(PrimaryObject primary) {
		if (!PlatformUI.getWorkbench().getActiveWorkbenchWindow()
				.getActivePage().equals(this)) {
			return null;
		}
		return super.getInput(primary);
	}
}
